﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace SvnMigration
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            SvnOptions options = GetSourceOptions();
            IEnumerable<CommitInfo> itemSource = null;
            try
            {
                itemSource = SvnProcessHelper.GetSvnLog(options);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.ToString());
            }
            revisionsListBox.ItemsSource = itemSource;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void selectAllButton_Click(object sender, RoutedEventArgs e)
        {
            revisionsListBox.SelectAll();
        }

        private void unselectAllButton_Click(object sender, RoutedEventArgs e)
        {
            revisionsListBox.UnselectAll();
        }

        private SvnOptions GetSourceOptions()
        {
            return new SvnOptions(sourceUsernameTextBox.Text, sourcePasswordTextBox.Text, sourceTextBox.Text);
        }

        private SvnOptions GetTargetOptions()
        {
            return new SvnOptions(targetUsernameTextBox.Text, targetPasswordTextBox.Text, targetTextBox.Text);
        }

        private void migrateButton_Click(object sender, RoutedEventArgs e)
        {
            var items = revisionsListBox.SelectedItems;
            foreach (var item in items)
            {
                SvnOptions targetOptions = GetTargetOptions();
                SvnProcessHelper.Checkout(targetOptions);
                string targetPath = Directory.GetCurrentDirectory() + '\\' + "target";
                CleanDirectory(targetPath);
                SvnOptions sourceOptions = GetSourceOptions();
                CommitInfo info = item as CommitInfo;
                SvnProcessHelper.Export(sourceOptions, info);
                SvnProcessHelper.Add(targetOptions,targetPath);
                SvnProcessHelper.Commit(targetOptions, info);
                DeleteDirectory(targetPath);
            }
        }

        private void DeleteDirectory(string targetPath)
        {
            foreach (string directoryName in Directory.GetDirectories(targetPath))
            {
                DeleteDirectory(directoryName);
            }
            string[] files = Directory.GetFiles(targetPath);
            foreach (var file in files)
            {
                FileInfo fInfo = new FileInfo(file);
                fInfo.IsReadOnly = false;
                File.Delete(file);
            }
            DirectoryInfo dirInfo = new DirectoryInfo(targetPath);
            dirInfo.Attributes = FileAttributes.Directory;
            Directory.Delete(targetPath, true);
        }

        private void CleanDirectory(string targetPath)
        {
            foreach (string directoryName in Directory.GetDirectories(targetPath).Where(item => !item.EndsWith(".svn")))
            {
                CleanDirectory(directoryName);
            }
            string[] files = Directory.GetFiles(targetPath);
            foreach (var file in files)
            {
                File.Delete(file);
            }
        }
    }
}
